package com.ytjj.qmyx.admin.controller;

import com.alibaba.fastjson.JSON;
import com.ytjj.common.api.CommonPage;
import com.ytjj.common.api.CommonResult;
import com.ytjj.common.enums.MsgTypeEnum;
import com.ytjj.common.utils.HttpClientUtil;
import com.ytjj.qmyx.admin.config.SupplyApiConfig;
import com.ytjj.qmyx.admin.model.request.SendSMSRequest;
import com.ytjj.qmyx.admin.model.request.SmsSenderRecordsRequest;
import com.ytjj.qmyx.admin.model.request.SmsTemplateRequest;
import com.ytjj.qmyx.admin.model.response.SmsPreviewContentResponse;
import com.ytjj.qmyx.admin.model.response.SmsSenderRecordsResponse;
import com.ytjj.qmyx.admin.model.response.SmsTemplateResponse;
import com.ytjj.qmyx.admin.model.sms.SmsTemplate;
import com.ytjj.qmyx.admin.service.SmsManageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 短信发送相关接口
 * @author jiawei
 * @date 2023年04月18日 15:03
 */
@Slf4j
@RestController
@RequestMapping("/smsManage")
public class SmsManageController {

    @Resource
    private SmsManageService smsManageService;
    @Resource
    private SupplyApiConfig supplyApiConfig;

    /**
     * 编辑新增短信模板
     * @param smsTemplate
     * @return
     */
    @RequiresPermissions("sms:template:addOrEditTemplate")
    @PostMapping("/addOrEditTemplate")
    public CommonResult addOrEditTemplate(@RequestBody SmsTemplate smsTemplate) {
        smsManageService.addOrEditTemplate(smsTemplate);
        return CommonResult.success();
    }

    /**
     * 获得所有的短信签名
     * @return
     */
    @GetMapping("/getAllSign")
    public CommonResult<List<String>> getAllSign() {
        List<String> allSign = smsManageService.getAllSign();
        return CommonResult.success(allSign);
    }

    /**
     * 修改模板状态
     * @param id 模板id
     * @param status 状态 0-禁用 1-启用
     * @return CommonResult<List<String>>
     */
    @GetMapping("/changeStatus")
    @RequiresPermissions("sms:template:changeStatus")
    public CommonResult<List<String>> changeStatus(Integer id,Integer status) {
        return smsManageService.changeStatus(id, status);
    }


    /**
     * 删除模板
     * @param id 模板id
     * @return CommonResult<List<String>>
     */
    @GetMapping("/delete")
    @RequiresPermissions("sms:template:delete")
    public CommonResult<List<String>> delete(Integer id) {
        return smsManageService.delete(id);
    }


    /**
     * 分页查询模板列表
     */
    @PostMapping("/getTemplateOnePage")
    public CommonResult<CommonPage<SmsTemplateResponse>> getTemplateOnePage(@RequestBody SmsTemplateRequest request) {
        return CommonResult.success(smsManageService.getTemplateOnePage(request));
    }


    /**
     * 分页查询短信手动发送记录
     */
    @PostMapping("/getSmsSendRecordsOnePage")
    public CommonResult<CommonPage<SmsSenderRecordsResponse>> getSmsSendRecordsOnePage(@RequestBody SmsSenderRecordsRequest request) {
        return CommonResult.success(smsManageService.getSmsSendRecordsOnePage(request));
    }

    /**
     * 分页查询所有发送记录
     */
    @PostMapping("/getSysSmsSendRecordsOnePage")
    @RequiresPermissions("smsRecord2")
    public CommonResult<CommonPage<SmsSenderRecordsResponse>> getSysSmsSendRecordsOnePage(@RequestBody SmsSenderRecordsRequest request) {
        return CommonResult.success(smsManageService.getSysSmsSendRecordsOnePage(request));
    }


    /**
     * 发送模板短信
     */
    @PostMapping("/userSendSmsByTemplate")
    public CommonResult<List<String>> userSendSmsByTemplate(@Validated @RequestBody SendSMSRequest request) {
        return CommonResult.success(smsManageService.userSendSmsByTemplate(request));
    }

    /**
     * 发送自定义短信
     */
    @RequiresPermissions("sms:send:userSendSmsFreedom")
    @PostMapping("/userSendSmsFreedom")
    public CommonResult<List<String>> userSendSmsFreedom(@Validated @RequestBody SendSMSRequest request) {
        return CommonResult.success(smsManageService.userSendSmsFreedom(request));
    }

    /**
     * 短信填写预览接口
     */
    @PostMapping("/previewSmsContent")
    public CommonResult<String> previewSmsContent(@RequestBody SendSMSRequest request) {
        return CommonResult.success(smsManageService.previewSmsContent(request));
    }


    /**
     * 导入excel发送短信预览
     * @param file 文件
     * @param smsType 短信类型
     * @param templateId 模板id
     * @return CommonResult<SmsPreviewContentResponse>
     */
    @PostMapping("/previewImportSmsContent")
    public CommonResult<SmsPreviewContentResponse> previewImportSmsContent(MultipartFile file,Integer smsType,Integer templateId) {
        return CommonResult.success(smsManageService.previewImportSmsContent(file,smsType,templateId));
    }

    /**
     * 导入excel批量发送短信
     */
    @RequiresPermissions("sms:send:sendImportSms")
    @PostMapping("/sendImportSms")
    public CommonResult<List<String>> sendImportSms(MultipartFile file,Integer smsType,Integer templateId) {
        return smsManageService.sendImportSms(file,smsType,templateId);
    }

    /**
     * 获取所有短信类型
     */
    @GetMapping("/getAllMsgType")
    public CommonResult getAllMsgType() {
        Map<Integer, String> allMsgType = MsgTypeEnum.getAllMsgType();
        return CommonResult.success(allMsgType);
    }




}
